package com.example.service.webhook.business.email.repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.service.webhook.business.carrier.entity.MobileSmsEntity;
import com.example.service.webhook.business.email.entity.EmailBillShoppingSheetEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
import com.example.service.webhook.util.SequenceUtil;
/**
 * 这里用的是jdbctemplate，DAO层可以根据自己的业务来选择
 * ClassName: BillRepository    
 * date: 2016年7月19日 下午6:29:23   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Repository
public class BillShoppingSheetRepository extends BaseDAO<EmailBillShoppingSheetEntity>{
	
	@Resource(name="templateEmail")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
    
   
    
    public void saveBillShoppingSheet(EmailBillShoppingSheetEntity entity,String userId) throws Exception  {
    	entity.setCreateTime(DateUtil.getCurrentDate());
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.saveAndReturnKey(entity, getTableName(userId));
    }
    
//public int[] batchSave(String userId, final List<EmailBillShoppingSheetEntity> sheetList) {
//		
//    	String sql="insert into "+getTableName(userId)+" (userId,mobile,billMonth,time,peerNumber,location,sendType,msgType,serviceName,fee,createTime,lastModifyTime) values"
//				+ " (?,?,?,?,?,?,?,?,?,?,?,?)";
//    	final java.sql.Timestamp d =  new java.sql.Timestamp(DateUtil.getCurrentDate().getTime());
//    	
//    	
//		
//    	int[] updateCounts = getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
//			 
//			@Override
//			public void setValues(PreparedStatement ps, int i) throws SQLException {
//				EmailBillShoppingSheetEntity entity = sheetList.get(i);
//				ps.setString(1, entity.getUserId());
//				ps.setString(2, entity.getMobile());
//				ps.setString(3,entity.getBillMonth());
//				ps.setString(4, entity.getTime());
//				ps.setString(5, entity.getPeerNumber());
//				ps.setString(6, entity.getLocation());
//				ps.setString(7, entity.getSendType());
//				ps.setString(8, entity.getMsgType());
//				ps.setString(9, entity.getServiceName());
//				ps.setInt(10, entity.getFee()==null?0:entity.getFee());
//				ps.setTimestamp(11, d);
//				ps.setTimestamp(12, d);
//			}
//		 
//			@Override
//			public int getBatchSize() {
//				return smsList.size();
//			}
//		  });
//    	return updateCounts;
//	}
    
    
    public void deleteShoppingSheetByBillId(String userId,String billId){
    	String sql = "delete from "+getTableName(userId)+" where billId=?";
    	this.delete(sql, new Object[]{billId});
    }
    
    
    private String getTableName(String userId){
    	return "t_bill_shoppingsheet_"+SequenceUtil.getTableSuffix(userId);
    }
}
